package com.njitweb.util;


import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.StatObjectArgs;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Configuration
//@ConfigurationProperties(prefix = "minio")
@Data
public class MinioTemplate {
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private MinioClient minioClient;

    //判断桶是否存在

    @SneakyThrows
    public void pubObject(String objectName, MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(bucketName).object(objectName)
                    .contentType(file.getContentType())
                    .stream(inputStream, file.getSize(), -1)
                    .build();
            minioClient.putObject(args);
        }
    }


//    public boolean isExist(String objectName) throws Exception {
//        StatObjectResponse statObjectResponse = minioClient.statObject(
//                StatObjectArgs.builder()
//                        .bucket(bucketName)
//                        .object(objectName)
//                        .build());
//        return statObjectResponse == null;
//    }


    //上传对象


    //删除对象


    //获取对象信息


    //返回的文件路径,不会过期.
}
